#ifndef _OUTPUT_LOG_H_
#define _OUTPUT_LOG_H_
#include "stdio.h"
#include "stm32f4xx.h"

#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"

#define UART_LOG_OUT 1

// #define UART1_USED  1
extern unsigned char g_debug_threshold;
extern unsigned char ELOG_OUTPUT_LVL;
extern uint8_t g_log_status;

extern QueueHandle_t xLogQueue;


extern StaticSemaphore_t xLogOutMutexBuffer;

// 互斥量句柄，用于访问互斥量。
extern SemaphoreHandle_t xLogOutMutex;

extern char *log_data_into_queue ;


//#define MSG_OUT(level, ...) \
//  message_out(level, __VA_ARGS__)

#define DEBUG_MSG(level, ...) \
  debug_message_out(level, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)

void output_log_uart_init(unsigned char debug_threshold);
void mutx_init_and_set_threshold(unsigned char level);
void set_threshold(unsigned char level);
void uart_send_char(char c);
void uart_send_string(const char *str);
void debug_message_out(unsigned char level, const char *file, int line, const char *func, const char *format, ...);
//void message_out(unsigned char level, const char *formate, ...);
void message_out(unsigned char level, const char *file, int line, const char *func, const char *format, ...);

#ifdef __cplusplus
extern "C"
{
#endif

  /* 日志级别 (与 message_out 的 level 参数对应) */
#define ELOG_LVL_ASSERT  0
#define ELOG_LVL_ERROR   1
#define ELOG_LVL_WARN    2
#define ELOG_LVL_INFO    3
#define ELOG_LVL_DEBUG   4
#define ELOG_LVL_VERBOSE 5

///* 设置日志级别 (默认输出 DEBUG 及以上级别) */
//#ifndef ELOG_OUTPUT_LVL
//#define ELOG_OUTPUT_LVL ELOG_LVL_DEBUG
//#endif

/* 日志宏定义 - 调用 message_out 函数 */
#define LOG_A(args, ...)                                                                              \
  do                                                                                                  \
  {                                                                                                   \
    if (ELOG_OUTPUT_LVL >= ELOG_LVL_ASSERT)                                                           \
    {                                                                                                 \
      message_out((unsigned char)ELOG_LVL_ASSERT, __FILE__, __LINE__, __func__, args, ##__VA_ARGS__); \
    }                                                                                                 \
  } while (0)

#define LOG_E(args, ...)                                                                             \
  do                                                                                                 \
  {                                                                                                  \
    if (ELOG_OUTPUT_LVL >= ELOG_LVL_ERROR)                                                           \
    {                                                                                                \
      message_out((unsigned char)ELOG_LVL_ERROR, __FILE__, __LINE__, __func__, args, ##__VA_ARGS__); \
    }                                                                                                \
  } while (0)

#define LOG_W(args, ...)                                                                            \
  do                                                                                                \
  {                                                                                                 \
    if (ELOG_OUTPUT_LVL >= ELOG_LVL_WARN)                                                           \
    {                                                                                               \
      message_out((unsigned char)ELOG_LVL_WARN, __FILE__, __LINE__, __func__, args, ##__VA_ARGS__); \
    }                                                                                               \
  } while (0)

#define LOG_I(args, ...)                                                                            \
  do                                                                                                \
  {                                                                                                 \
    if (ELOG_OUTPUT_LVL >= ELOG_LVL_INFO)                                                           \
    {                                                                                               \
      message_out((unsigned char)ELOG_LVL_INFO, __FILE__, __LINE__, __func__, args, ##__VA_ARGS__); \
    }                                                                                               \
  } while (0)

#define LOG_D(args, ...)                                                                             \
  do                                                                                                 \
  {                                                                                                  \
    if (ELOG_OUTPUT_LVL >= ELOG_LVL_DEBUG)                                                           \
    {                                                                                                \
      message_out((unsigned char)ELOG_LVL_DEBUG, __FILE__, __LINE__, __func__, args, ##__VA_ARGS__); \
    }                                                                                                \
  } while (0)

#define LOG_V(args, ...)                                                                               \
  do                                                                                                   \
  {                                                                                                    \
    if (ELOG_OUTPUT_LVL >= ELOG_LVL_VERBOSE)                                                           \
    {                                                                                                  \
      message_out((unsigned char)ELOG_LVL_VERBOSE, __FILE__, __LINE__, __func__, args, ##__VA_ARGS__); \
    }                                                                                                  \
  } while (0)

#ifdef __cplusplus
}
#endif

#endif
